home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / science / sm32a.zip / SYMBMATH.H35 < prev    next >
Text File  |  1994-03-28  |  6KB  |  184 lines

  1.     4.3      Defining Your Own Functions, Procedures and Rules
  2.     4.3.1    Defining Your Own Functions
  3.  
  4.     Anytime when you find yourself using the same expression over  
  5. and over, you should turn it into a function.  
  6.     You can define your own functions by
  7.            f(x_) := x^2
  8.     Here are some sample function definitions:
  9.            f(x_) := cos(x + pi/3)
  10.            g(x_, y_) := x^2 - y^2
  11.     The arguement in the definition should be the pattern x_. 
  12. Otherwise f() works only for a specific symbolic value, e.g. x when 
  13. defining f(x):=x^2. The pattern x_ should be only on the left side of
  14. the assignment.
  15.        Once defined, functions can be used in expressions or in other 
  16. function definitions:
  17.            y := f(3.2)
  18.            z := g(4.1, -5.3)
  19.  
  20.     Example 4.3.1.
  21. Define a new function for x^2, then evaluate it.
  22.     
  23. IN:  g(x) := x^2
  24. IN:  g(2), g(a),  g(x)
  25. OUT: g(2), g(a),  x^2         # work only for a symbolic value x
  26. IN:  f(x_) := x^2
  27. IN:  f(2), f(a)
  28. OUT: 4, a^2                   # work for any value
  29.  
  30.                 4.3.1.1  Defining Conditional Functions
  31.     You can define a conditional function by the if() function:
  32.         f(x_) := if(x>0 then  1)
  33.         f(x_) := if(x>0 then x^2 else x)
  34. or by inequalities:
  35.                 f(x_) := x>0
  36.         f(x_) := (x>0) * x^2 + (x<=0) * x
  37. On the first definition by if(), when f() is called it gives 1 if x>0,  
  38. or left unevaluated otherwise. On the second definition by the if(), when
  39. f() is called it gives x^2 if x>0, x if x<=0, or left unevaluated
  40. otherwise. On the third definision by the inequality, when f() is called,
  41. it gives 1 for x>0, 0 for x<=0, or x>0 for symbolic value of x.
  42. On the last definition, when f() is called, it is evaluated for any
  43. numeric or symbolic value of x.
  44.     Remember that the words "then" and "else" can be replaced by 
  45. comma ,.
  46.     You cannot differentiate nor integrate the conditional function
  47. defined by if(), but you can do the conditional functions defined by
  48. inequalities.
  49.  
  50.         You can define a function evaluated only for numbers by
  51.         f(x_) := if(isnumber(x) then x^2)
  52. This definition is different from the definition by f(x_) := x^2. On the
  53. latter, when f() is called, it gives x^2, regardless whatever x is. On the
  54. former, when f() is called, it gives x^2 if x is a number, or left
  55. unevaluated otherwise.
  56.     
  57.     Example 4.3.2.
  58. evaluate to x^2 only if x is number, by defining a conditional function.
  59.  
  60. IN:  f(x_) := if(isnumber(x) then x^2)
  61. IN:  f(2), f(a)
  62. OUT: 4, f(a)
  63.  
  64. IN:  f(x_) := if(x>0 then x^2)
  65. IN:  f(2), f(-2), f(a)
  66. OUT: 4, f(-2), f(a)
  67.  
  68. IN:  f(x_) := if(x>0 then x^2 else x)
  69. IN:  f(2), f(-2), f(a)
  70. OUT: 4, 2, f(a)
  71.  
  72.            4.3.1.2  Defining Case Functions
  73.     You can define the case function by different pattern name. The
  74. case function is similar to the case statement in BASIC language.
  75.     Example:
  76.  
  77. IN:  f(x_) := if( x > 0 and x < 1 then 1)
  78. IN:  f(u_) := if( u > 1 and u < 2 then 2)
  79. IN:  f(v_) := if( v > 2 and v < 3 then 3)
  80. IN:  f(0.2), f(1.2), f(2.2)
  81. OUT: 1, 2, 3
  82.  
  83.            4.3.1.3  Defining Piece-wise Functions
  84.         You can define a piece-wise function.
  85.     Example 4.3.3. 
  86.         define
  87.  
  88.       / x       if x < 0
  89. f(x) =  0       if x = 0
  90.       \ x^2     if x > 0
  91.  
  92. then evaluate f(-2), f(0), f(3), f(a), f'(x), diff(f(x), x=3).
  93.  
  94. IN:  f(x_) := x*(x<0)+x^2*(x>0)
  95. IN:  f(-2), f(0), f(3), f(a)
  96. OUT: -2, 0, 9, (a < 0) a + (a > 0) a^2
  97. IN:  f'(x)
  98. OUT: (x < 0) + 2 x (x > 0)
  99. IN:  diff(f(x), x=3)
  100. OUT: 6
  101.  
  102.                 4.3.1.4  Defining Recursion Functions
  103.         You can define a recursion function.
  104.         Example.
  105. IN:  factoria(1) := 1
  106. IN:  factoria(n_) := if(n > 1, (n-1)*factoria(n-1))
  107.  
  108.                  4.3.1.5  Defining Multi-Value Functions
  109.         You can define a function with the multi function values.
  110.         Example.
  111. IN:  squreroot(x_) := [sqrt(x), -sqrt(x)]
  112. IN:  squreroot(4)
  113. OUT: [2, -2]
  114.  
  115.     Anytime when you find yourself using the same definition over  
  116. and over, you should turn it into a library.  
  117.     You can make your defined function as if the built-in function,
  118. by saving your definition into disk file as a library with the function
  119. name plus extension .(x) as the filename. e.g. saving above factoria
  120. function as the factoria.(x) file (see Section 3.6 Libraies and Packages).
  121.  
  122.         4.5.2  Defining Your Own Procedures
  123.     You can define a function as a procedure by
  124.                 f(x_) := block(command1, command2, ..., commadN)
  125.                 f(x_) := block(command1, command2, ..., commandN, local(a))
  126.  
  127.     By default, all variables within procedure are global, except for
  128. variables declared by local(). The mult-statement should be grouped by
  129. block(). The block() only outputs the result of the last statement or the
  130. second last one as its value. The mult-line must be teminated by a comma,
  131. (not by a comma and a blank space). Local() must be the last one in
  132. block().
  133.  
  134.     Example 4.5.2. 
  135. define a numeric integration procedure ninte() and calculate integral
  136. of x^2 from x=1 to x=2 by call ninte().
  137.  
  138. IN:  ninte(y_,x_,a_,b_) := block( numeric:=on,
  139.         dd:=(b-a)/50,
  140.     aa:=a+dd,
  141.     bb:=b-dd,
  142.     y0:=subs(y, x = a),
  143.     yn:=subs(y, x = b),
  144.     (sum(y,x,aa,bb,dd)+(y0+yn)/2)*dd,
  145.         local(dd,aa,bb,y0,yn) )
  146. IN:  ninte(x^2, x from 1 to 2)
  147.  
  148.  
  149.         4.5.3  Defining Your Own Rules
  150.     You can define transform rules. Defining rules is similar to 
  151. defining functions. In defining functions, all arguments must be simple
  152. variables, but in defining rules, the first argument can be a
  153. complicated expression.
  154.     
  155.        Example 4.5.3.1 
  156.        define log rules.
  157. IN:  log(x_ * y_) := log(x) + log(y)
  158. IN:  log(x_ ^ n_) := n*log(x)
  159. IN:  log(a*b)
  160. OUT: log(a) + log(b)
  161.  
  162.         Example 4.5.3.2
  163. IN:  sin(-x_) := -sin(x)
  164. IN:  sin(-a)
  165. OUT: -sin(a)
  166.  
  167.         Example 4.5.3.3
  168.         define the trig simplification rules.
  169. IN:  simplify(sin(x_)^2, x_) := 1/2*(1-cos(x))
  170. IN:  simplify(sin(x)^2,x)
  171. OUT: 1/2 (1 - cos(x))
  172.  
  173.     Example 4.5.3.4 
  174.         define Laplace transform rules.
  175. IN:  laplace(sin(t_), t_) := 1/(t^2+1)
  176. IN:  laplace(sin(s), s)
  177. OUT: 1/(s^2 + 1)
  178.  
  179.         Example 4.5.3.5
  180.         define derivative (see Chapter 4.5.2 Defining f'(x) for detail).
  181. IN:  f'(x_) := sin(x)
  182. IN:  f'(t)
  183. OUT: sin(t)
  184.